package com.wiboson.money.DataSource;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wiboson.money.Base.NetWork;
import com.wiboson.money.DataSource.model.KLine;
import org.apache.commons.lang.StringEscapeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by moon on 17/4/28.
 */
public class historySource {

    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");

    /**
     * http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?_var=kline_day2017&param=sz399300,day,2017-01-01,2018-12-31,640,&r=0.56298635515264103
     *
     * http://api.finance.ifeng.com/akdaily/?code=sz399300&type=last
     */
    public static void main(String args[]){

        //NowInfo nowInfo= new NowInfo();
        //nowInfo.buildBySina(NetWork.sendGet("http://hq.sinajs.cn/list=sh601006"));
        //System.out.println(NetWork.sendGet("http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?&rand=random(10000)&symbol=sh600000&end_date=20150809&begin_date=20000101&type=plain"));
        //System.out.println(nowInfo);
        //List<KLine> lines = getK(new Date(117,3,1),new Date(117,3,10),"day",'2',399300);
        List<KLine> lines = getHistory('2',399300);
        for(KLine x:lines){
            System.out.println(x);
        }

    }

    public static List<KLine> getHistory(char type, int num){
        String url;
        if(type=='1'){
            url = "http://api.finance.ifeng.com/akdaily/?code=sh"+String.format("%06d", num)+"&type=last";
        }else if(type=='2'){
            url = "http://api.finance.ifeng.com/akdaily/?code=sz"+String.format("%06d", num)+"&type=last";
        }else {
            return null;
        }
        System.out.println(url);
        try {
            List<KLine> lines = new ArrayList<KLine>();
            String reply = NetWork.sendGet(url,"UTF-8");
            if(reply.length()<30){
                return null;
            }
            //System.out.println(reply);
            JSONObject object = JSON.parseObject(reply);
            JSONArray day = object.getJSONArray("record");
            for(int i =0;i<day.size();i++){
                lines.add(new KLine().buildByHistory(((List)day.get(i))));
            }
            return lines;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("错误股票："+type+"|"+String.format("%06d", num));
        }

        return null;
    }


    public static List<KLine> getK(Date begindate, Date enddate,String timetype, char type, int num){
        String url = "http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?_var=kline_day";
        String sty;
        if(begindate.getYear()==enddate.getYear()){
            url+=yearFormat.format(begindate);
            if(type=='1'){
                url+= "&param=sh";
                sty = "sh";
            }else if(type=='2'){
                url+= "&param=sz";
                sty = "sz";
            }else {
                return null;
            }
            url+= (String.format("%06d", num)+","+timetype+","+dateFormat.format(begindate)+","+dateFormat.format(enddate)+",640,&r="+Math.random());
            String reply = StringEscapeUtils.unescapeJava(NetWork.sendGet(url,"UTF-8"));
            reply = reply.substring(reply.indexOf("{"),reply.length());
            try {
                JSONObject out = JSON.parseObject(reply);
                List<KLine> lines = new ArrayList<KLine>();
                if(out.getInteger("code")==0){
                JSONObject data = out.getJSONObject("data").getJSONObject(sty+num);
                JSONArray dayly = data.getJSONArray("day");
                    for(int i =0;i<dayly.size();i++){
                        lines.add(new KLine().buildByK(((List)dayly.get(i))));
                    }
                    return lines;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }



}
